package com.mantru.iotpot.Adapter.Base

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView


@SuppressLint("NotConstructor")
abstract class AppAdapter<T>(var mContext: Context, @LayoutRes layoutResId: Int, data: List<T>?) :
    RecyclerView.Adapter<AppHolder>() {
    var mLayoutRes = 0
    var mItems: List<T>

    init {
        mItems = data ?: ArrayList()
        if (layoutResId != 0) {
            mLayoutRes = layoutResId
        }
        
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppHolder {
        return AppHolder(LayoutInflater.from(mContext).inflate(mLayoutRes, parent, false))
    }

    override fun onBindViewHolder(holder: AppHolder, position: Int) {
        mItems[position]?.let { convert(holder, position, it) }
        
    }

    override fun getItemCount(): Int {
        return mItems.size
    }

    protected abstract fun convert(holder: AppHolder?, position: Int, item: T)
}
